import { Component, OnInit, OnDestroy } from '@angular/core'
import { NzMessageService } from 'ng-zorro-antd'
import { Router, ActivatedRoute } from '@angular/router'

import { PageService } from '@shared/services/page.service'
import { INewPageColumn } from '@shared/interfaces/common'
import { ComWindow } from '@shared/classes/ComWindowClass'
import { TerminalNewBaseComponent } from './base/base.component'
import { TerminalNewSystemComponent } from './system/system.component'
import { TerminalNewTradeComponent } from './trade/trade.component'
import { TerminalService } from '../terminal.abstract.service'
import { TerminalNewRequest } from '@shared/interfaces/terminal'
import { TerminalStoreService } from './terminal-store.service'
import { I18NService } from '@core'

@Component({
  selector: 'app-terminal-new',
  templateUrl: './new.component.html',
  styles: [],
})
export class TerminalNewComponent implements OnInit, OnDestroy {
  mode = 1
  pages: INewPageColumn[] = []

  constructor(
    public msg: NzMessageService,
    public router: Router,
    public route: ActivatedRoute,
    public terminalService: TerminalService,
    private pageService: PageService,
    private terminalStore: TerminalStoreService,
    private i18n: I18NService,
  ) {}

  ngOnInit() {
    this.initColumns()
  }

  ngOnDestroy(): void {
    this.terminalStore.reset()
  }

  initColumns() {
    this.pages = [
      {
        component: new ComWindow(TerminalNewSystemComponent, { title: 'GENERAL.INFO.OTHER.SYSTEM_INFO' }),
      },
      {
        component: new ComWindow(TerminalNewBaseComponent, { title: 'GENERAL.INFO.OTHER.BASIC_CONFIG' }),
      },
      // {
      //   component: new ComWindow(TerminalNewTradeComponent, { title: this.trans('GENERAL.INFO.OTHER.TRADE_INFO') }),
      // }
    ]
  }

  submit(): void {
    const { count, intStoreCode, termVendar, termCodes, isAmountFixed, fixedAmt, remark } = this.terminalStore.value
    const requestData: TerminalNewRequest = {
      intStoreCode,
      count,
      termCodes,
      isAmountFixed,
      fixedAmt,
      remark,
      termVendar
    }
    this.terminalService.createTerminal(requestData).subscribe(isSuccess => {
      if (isSuccess) {
        this.pageService.triggerGuardAlive(false)
        this.terminalStore.reset()
        this.msg.success(this.trans('GENERAL.NOTIFICATION.CREATE_SUCCESS'))
        this.router.navigate(['../'], { relativeTo: this.route})
      }
    }, (err) => {
      console.log('err',  err)
    })
  }

  trans(key: string, params?: Object): string {
    return this.i18n.fanyi(key, params)
  }
}
